package com.txx.app.main.modules.web.function;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.rsq.function.txxpluginsdk.user.bean.TXXUser;
import com.txx.app.main.App;
import com.txx.app.main.commom.bean.H5UserInfoBean;
import com.txx.app.main.modules.web.bean.H5GetUserInfoResult;
import com.txx.app.main.modules.web.js.H5BasePlugin;
import com.txx.app.main.modules.web.js.H5Event;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.utils.SystemUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * created by houyl
 * on  6:51 PM
 */
public class H5GetUsersInfoByNative extends H5BasePlugin {

    @Override
    public boolean handleEvent(H5Event h5Event, WebCommonFragment fragment) {
        if (h5Event == null) {
            return false;
        }
        try {

            String json = h5Event.getJsonSource ();
            JSONObject jsonObject = new JSONObject (json);
            String callBackName = jsonObject.getString ("callBackName");
            //        处理后调用
            if (fragment != null) {
                fragment.getH5CallBackCommon (callBackName, new Gson ().toJson (getH5UserInfo ()));
            }
        } catch (JSONException e) {
            e.printStackTrace ();
        } catch (Exception e) {
            e.printStackTrace ();
        }

        return true;
    }

    @Override
    public void onRelease() {

    }

    @Override
    public String getCallBackName() {
        return null;
    }


    public H5GetUserInfoResult getH5UserInfo() {
        H5GetUserInfoResult h5GetUserInfoResult = new H5GetUserInfoResult ();
        if (TextUtils.isEmpty (App.staffBuidler) && TextUtils.isEmpty (App.TOKEN)) {
            h5GetUserInfoResult.setCode ("-100");
            h5GetUserInfoResult.setDesc ("获取用户信息失败");
            H5UserInfoBean bean = new H5UserInfoBean ();
            h5GetUserInfoResult.setMsg (bean);
        } else {
            TXXUser txxUser = App.perInfo;
            h5GetUserInfoResult.setCode ("0");
            h5GetUserInfoResult.setDesc ("成功获取用户信息");
            H5UserInfoBean bean = new H5UserInfoBean ();
            bean.setGender (txxUser.getGender ());
            bean.setStaffNumber (App.staffBuidler);
            bean.setChannel (txxUser.getChannel ());
            bean.setCompanyCode (txxUser.getOrgCode ());
            bean.setRoles (txxUser.getRoles ());
            bean.setUserCode (txxUser.getStaffNumber ());
            bean.setUserName (txxUser.getName ());
            bean.setUcmCompanyCode (txxUser.getUcmCompanyCode ());
            bean.setUcmEmployNo (txxUser.getUcmEmployNo ());
            bean.setPhone (txxUser.getMobile ());
            bean.setBranchType (txxUser.getBranchType ());
            bean.setReceiptno (txxUser.getReceiptno ());
            bean.setOrgDesc (txxUser.getOrgDesc ());
            bean.setAgtype (txxUser.getAgtype ());
            bean.setAgntbr (txxUser.getAgntbr ());
            bean.setAracde (txxUser.getAracde ());
            bean.setToken (App.TOKEN);
            bean.setImei (SystemUtils.getDeviceImei ());
            bean.setTkStartLevel(txxUser.getTkStartLevel());
            bean.setF1Level(txxUser.getF1Level());
            bean.setTaiShow(txxUser.getTaiShow());

            h5GetUserInfoResult.setMsg (bean);
        }
        return h5GetUserInfoResult;
    }


}
